package liu.per.jvms;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuj on 2015/4/19.
 * VM args : -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
 */
@Result("java.lang.OutOfMemoryError: Java heap space\n" +
        "Dumping heap to java_pid9308.hprof ...\n" +
        "Heap dump file created [28134096 bytes in 0.143 secs]\n" +
        "Exception in thread \"main\" java.lang.OutOfMemoryError: Java heap space\n" +
        "\tat java.util.Arrays.copyOf(Arrays.java:2245)\n" +
        "\tat java.util.Arrays.copyOf(Arrays.java:2219)\n" +
        "\tat java.util.ArrayList.grow(ArrayList.java:242)\n" +
        "\tat java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)\n" +
        "\tat java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)\n" +
        "\tat java.util.ArrayList.add(ArrayList.java:440)\n" +
        "\tat liu.per.jvms.TestOOM.main(TestOOM.java:20)\n" +
        "\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
        "\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\n" +
        "\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n" +
        "\tat java.lang.reflect.Method.invoke(Method.java:606)\n" +
        "\tat com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)")
@Analyzes("use jhat java_pid9308.hprof,会在本机生成一个7000的http端口，访问http://localhost:7000/可以看到每个类有多少个对象" +
        "比如http://localhost:7000/showInstanceCounts/includePlatform/ 可以看看到" +
        "810326 instances of class liu.per.jvms.TestOOM$OOMObj \n" +
        "2262 instances of class [C \n" +
        "2105 instances of class java.lang.String \n" +
        "785 instances of class java.util.TreeMap$Entry " +
        "一共生成了810326个OOMObj对象，以此为依据，可以看出你的程序是否在对象数上有明显异常")
public class TestOOM {

    static class OOMObj {

    }

    public static void main(String[] args) {

        List<OOMObj> list = new ArrayList<>();
        while (true) {
            list.add(new OOMObj());
        }

    }



}


